{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "chat = ChatOpenAI(model=\"lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF\",base_url=\"http://localhost:1234/v1\", api_key=\"lm-studio\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'有一只猫想去上学，但它没有钱买书，所以它就去找了个聪明的老鼠。老鼠说：“没问题，我会教你读书。”于是，老鼠教猫认字。第一天，猫学到了“dog”（狗）。第二天，猫又学会了“cat”（猫）。第三天，猫告诉老鼠：“我已经学会了两种动物的名字了！”老鼠说：“不够啊，我们还要学习更多的知识。”'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import HumanMessage, SystemMessage\n",
    "\n",
    "messages = [\n",
    "    SystemMessage(content=\"你是一个乐于助人的助手\"),\n",
    "    HumanMessage(content=\"讲一个笑话?\"),\n",
    "]\n",
    "# for chunk in chat.stream(messages):\n",
    "#     print(chunk.content, end=\"\", flush=True)\n",
    "# ai_message = ''\n",
    "# for chunk in chat.stream(messages):\n",
    "#     ai_message += chunk.content\n",
    "# print(ai_message)\n",
    "chat.invoke(messages).content\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain0_1",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
